package com.shareppy.holidays.co;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.RemoteViews;

import com.shareppy.utils.Holiday;
import com.shareppy.utils.Holidays;

public class HolidaysWidgetProvider extends AppWidgetProvider {

	public static final String HOLIDAYSWIDGET_UPDATE = "HOLIDAYSWIDGET_UPDATE";
	public static final String DEFAULT_UPDATE = "android.appwidget.action.APPWIDGET_UPDATE";
	private static AlarmManager alarmManager;
	private static PendingIntent pendingIntent;

	private static SimpleDateFormat formatter = null;

	// Leidy Joya

	public static SimpleDateFormat getFormatter(Context context) {
		if (formatter == null) {
			formatter = new SimpleDateFormat(
					context.getString(R.string.date_format),
					Locale.getDefault());

		}
		return formatter;
	}

	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
		if (alarmManager != null) {
			// Toast.makeText(context, "Se cancela la alarma...",
			// Toast.LENGTH_LONG).show();
			alarmManager.cancel(pendingIntent);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		// Toast.makeText(context, "onReceive()" + intent.getAction(),
		// Toast.LENGTH_LONG).show();
		if (HOLIDAYSWIDGET_UPDATE.equals(intent.getAction())
				|| DEFAULT_UPDATE.equals(intent.getAction())) {
			Bundle extras = intent.getExtras();
			if (extras != null) {
				AppWidgetManager appWidgetManager = AppWidgetManager
						.getInstance(context);
				ComponentName thisAppWidget = new ComponentName(
						context.getPackageName(),
						HolidaysWidgetProvider.class.getName());
				int[] appWidgetIds = appWidgetManager
						.getAppWidgetIds(thisAppWidget);
				onUpdate(context, appWidgetManager, appWidgetIds);
			}
		}
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		final int N = appWidgetIds.length;
		for (int i = 0; i < N; i++) {
			int appWidgetId = appWidgetIds[i];
			updateWidget(context, appWidgetManager, appWidgetId);
		}
	}

	public static void updateWidget(Context context,
			AppWidgetManager appWidgetManager, int appWidgetId) {
		Calendar currentDate = Calendar.getInstance();
		Holiday today = new Holiday(currentDate);
		RemoteViews remoteView = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);

		remoteView.setTextViewText(
				R.id.today_label,
				context.getString(R.string.today,
						getFormatter(context).format(currentDate.getTime())));
		remoteView.setTextViewText(R.id.is_holiday_label,
				context.getString(R.string.is_holiday_on_colombia));

		boolean holiday = Holidays.isHoliday(currentDate);
		if (holiday) {
			remoteView.setTextColor(R.id.yes_or_no_label, context
					.getResources().getColor(R.color.yes_color));
			remoteView.setTextViewText(R.id.yes_or_no_label,
					context.getString(R.string.yes));
		} else {
			remoteView.setTextColor(R.id.yes_or_no_label, context
					.getResources().getColor(R.color.no_color));
			remoteView.setTextViewText(R.id.yes_or_no_label,
					context.getString(R.string.no));

			Holiday nextHoliday = Holidays.getNextHoliday(currentDate,
					currentDate.get(Calendar.YEAR));
			int days = (int) ((nextHoliday.toCalendar().getTimeInMillis() - currentDate
					.getTimeInMillis()) / 1000 / 3600 / 24);
			remoteView.setTextViewText(
					R.id.next_holiday_label,
					context.getString(
							R.string.next_holiday,
							String.valueOf(days),
							getFormatter(context).format(
									nextHoliday.toCalendar().getTime())));
		}

		Uri uri = Uri.parse(today.toWikipediaLink());
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		PendingIntent pIntent = PendingIntent.getActivity(context, appWidgetId,
				intent, 0);
		// viewID - our clickable view ID
		remoteView.setOnClickPendingIntent(R.id.yes_or_no_label, pIntent);

		appWidgetManager.updateAppWidget(appWidgetId, remoteView);
	}

	public static AlarmManager getAlarmManager() {
		return alarmManager;
	}

	public static void setAlarmManager(AlarmManager alarmManager) {
		HolidaysWidgetProvider.alarmManager = alarmManager;
	}

	public static PendingIntent getPendingIntent() {
		return pendingIntent;
	}

	public static void setPendingIntent(PendingIntent pendingIntent) {
		HolidaysWidgetProvider.pendingIntent = pendingIntent;
	}

}